{% extends 'base.html' %}

{% block title %}{{ task.name }} - 定时任务详情 - EasyTesting{% endblock %}

{% block header %}定时任务详情{% endblock %}

{% block header_buttons %}
<div class="btn-group">
    <a href="{% url 'scheduled_task_edit' pk=task.pk %}" class="btn btn-outline-secondary">
        <i class="bi bi-pencil"></i> 编辑
    </a>
    <button type="button" class="btn btn-outline-success" onclick="runTaskNow({{ task.pk }})">
        <i class="bi bi-play-fill"></i> 立即执行
    </button>
    <button type="button" class="btn {% if task.status == 'active' %}btn-outline-warning{% else %}btn-outline-info{% endif %}" 
            onclick="toggleTaskStatus({{ task.pk }})">
        <i class="bi bi-{% if task.status == 'active' %}pause{% else %}play{% endif %}"></i> 
        {% if task.status == 'active' %}暂停{% else %}激活{% endif %}
    </button>
</div>
{% endblock %}

{% block content %}
<div class="row">
    <div class="col-md-8">
        <!-- 基本信息 -->
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="mb-0">基本信息</h5>
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="col-md-6">
                        <table class="table table-borderless">
                            <tr>
                                <th width="30%">任务名称:</th>
                                <td>{{ task.name }}</td>
                            </tr>
                            <tr>
                                <th>测试套件:</th>
                                <td>
                                    <a href="{% url 'test_suite_detail' pk=task.test_suite.pk %}">
                                        {{ task.test_suite.name }}
                                    </a>
                                </td>
                            </tr>
                            <tr>
                                <th>执行环境:</th>
                                <td>{{ task.environment.name }}</td>
                            </tr>
                            <tr>
                                <th>状态:</th>
                                <td>
                                    {% if task.status == 'active' %}
                                    <span class="badge bg-success">{{ task.get_status_display }}</span>
                                    {% elif task.status == 'paused' %}
                                    <span class="badge bg-warning">{{ task.get_status_display }}</span>
                                    {% else %}
                                    <span class="badge bg-secondary">{{ task.get_status_display }}</span>
                                    {% endif %}
                                </td>
                            </tr>
                        </table>
                    </div>
                    <div class="col-md-6">
                        <table class="table table-borderless">
                            <tr>
                                <th width="30%">调度类型:</th>
                                <td>{{ task.get_schedule_type_display }}</td>
                            </tr>
                            <tr>
                                <th>上次执行:</th>
                                <td>
                                    {% if task.last_run_time %}
                                    {{ task.last_run_time|date:"Y-m-d H:i:s" }}
                                    {% else %}
                                    <span class="text-muted">从未执行</span>
                                    {% endif %}
                                </td>
                            </tr>
                            <tr>
                                <th>下次执行:</th>
                                <td>
                                    {% if task.next_run_time %}
                                    <span class="text-primary">{{ task.next_run_time|date:"Y-m-d H:i:s" }}</span>
                                    {% else %}
                                    <span class="text-muted">未安排</span>
                                    {% endif %}
                                </td>
                            </tr>
                            <tr>
                                <th>创建人:</th>
                                <td>{{ task.created_by.username }}</td>
                            </tr>
                        </table>
                    </div>
                </div>
                
                {% if task.description %}
                <div class="mt-3">
                    <strong>描述:</strong>
                    <p class="mt-2">{{ task.description }}</p>
                </div>
                {% endif %}
            </div>
        </div>

        <!-- 执行历史 -->
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0">执行历史</h5>
            </div>
            <div class="card-body">
                {% if logs_page %}
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>开始时间</th>
                                <th>状态</th>
                                <th>执行时长</th>
                                <th>测试结果</th>
                                <th>成功率</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for log in logs_page %}
                            <tr>
                                <td>{{ log.start_time|date:"m-d H:i:s" }}</td>
                                <td>
                                    {% if log.status == 'success' %}
                                    <span class="badge bg-success">成功</span>
                                    {% elif log.status == 'failed' %}
                                    <span class="badge bg-danger">失败</span>
                                    {% elif log.status == 'running' %}
                                    <span class="badge bg-primary">运行中</span>
                                    {% else %}
                                    <span class="badge bg-secondary">{{ log.get_status_display }}</span>
                                    {% endif %}
                                </td>
                                <td>
                                    {% if log.duration %}
                                    {{ log.duration|floatformat:2 }}s
                                    {% else %}
                                    -
                                    {% endif %}
                                </td>
                                <td>
                                    {% if log.total_test_cases > 0 %}
                                    <small>
                                        总计: {{ log.total_test_cases }}<br>
                                        通过: <span class="text-success">{{ log.passed_test_cases }}</span><br>
                                        失败: <span class="text-danger">{{ log.failed_test_cases }}</span>
                                    </small>
                                    {% else %}
                                    -
                                    {% endif %}
                                </td>
                                <td>
                                    {% if log.total_test_cases > 0 %}
                                    <span class="{% if log.success_rate >= 80 %}text-success{% elif log.success_rate >= 60 %}text-warning{% else %}text-danger{% endif %}">
                                        {{ log.success_rate|floatformat:1 }}%
                                    </span>
                                    {% else %}
                                    -
                                    {% endif %}
                                </td>
                                <td>
                                    {% if log.test_run %}
                                    <a href="{% url 'test_run_detail' pk=log.test_run.pk %}" class="btn btn-sm btn-outline-primary">
                                        <i class="bi bi-eye"></i> 查看
                                    </a>
                                    {% endif %}
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
                
                {% include 'pagination.html' with page_obj=logs_page %}
                {% else %}
                <div class="text-center py-4">
                    <i class="bi bi-clock-history display-4 text-muted mb-3"></i>
                    <h6>暂无执行记录</h6>
                    <p class="text-muted">该任务还没有执行过</p>
                </div>
                {% endif %}
            </div>
        </div>
    </div>

    <div class="col-md-4">
        <!-- 统计信息 -->
        <div class="card mb-4">
            <div class="card-header">
                <h6 class="mb-0">执行统计</h6>
            </div>
            <div class="card-body">
                <div class="row text-center">
                    <div class="col-4">
                        <div class="border-end">
                            <h4 class="mb-1">{{ task.total_runs }}</h4>
                            <small class="text-muted">总执行次数</small>
                        </div>
                    </div>
                    <div class="col-4">
                        <div class="border-end">
                            <h4 class="mb-1 text-success">{{ task.successful_runs }}</h4>
                            <small class="text-muted">成功次数</small>
                        </div>
                    </div>
                    <div class="col-4">
                        <h4 class="mb-1 text-danger">{{ task.failed_runs }}</h4>
                        <small class="text-muted">失败次数</small>
                    </div>
                </div>
                
                {% if task.total_runs > 0 %}
                <div class="mt-3">
                    <div class="d-flex justify-content-between">
                        <small>成功率</small>
                        <small>{{ task.success_rate|floatformat:1 }}%</small>
                    </div>
                    <div class="progress" style="height: 6px;">
                        <div class="progress-bar {% if task.success_rate >= 80 %}bg-success{% elif task.success_rate >= 60 %}bg-warning{% else %}bg-danger{% endif %}" 
                             style="width: {{ task.success_rate }}%"></div>
                    </div>
                </div>
                {% endif %}
            </div>
        </div>

        <!-- 调度配置 -->
        <div class="card mb-4">
            <div class="card-header">
                <h6 class="mb-0">调度配置</h6>
            </div>
            <div class="card-body">
                <table class="table table-sm table-borderless">
                    <tr>
                        <th width="40%">类型:</th>
                        <td>{{ task.get_schedule_type_display }}</td>
                    </tr>
                    {% if task.schedule_type == 'daily' and task.scheduled_time %}
                    <tr>
                        <th>执行时间:</th>
                        <td>{{ task.scheduled_time|time:"H:i" }}</td>
                    </tr>
                    {% elif task.schedule_type == 'weekly' and task.weekday and task.scheduled_time %}
                    <tr>
                        <th>星期几:</th>
                        <td>周{{ task.weekday }}</td>
                    </tr>
                    <tr>
                        <th>执行时间:</th>
                        <td>{{ task.scheduled_time|time:"H:i" }}</td>
                    </tr>
                    {% elif task.schedule_type == 'monthly' and task.day_of_month and task.scheduled_time %}
                    <tr>
                        <th>每月第几天:</th>
                        <td>{{ task.day_of_month }}日</td>
                    </tr>
                    <tr>
                        <th>执行时间:</th>
                        <td>{{ task.scheduled_time|time:"H:i" }}</td>
                    </tr>
                    {% elif task.schedule_type == 'cron' and task.cron_expression %}
                    <tr>
                        <th>Cron表达式:</th>
                        <td><code>{{ task.cron_expression }}</code></td>
                    </tr>
                    {% elif task.schedule_type == 'once' and task.scheduled_date and task.scheduled_time %}
                    <tr>
                        <th>执行日期:</th>
                        <td>{{ task.scheduled_date|date:"Y-m-d" }}</td>
                    </tr>
                    <tr>
                        <th>执行时间:</th>
                        <td>{{ task.scheduled_time|time:"H:i" }}</td>
                    </tr>
                    {% endif %}
                </table>
            </div>
        </div>

        <!-- 通知配置 -->
        <div class="card">
            <div class="card-header">
                <h6 class="mb-0">通知配置</h6>
            </div>
            <div class="card-body">
                <table class="table table-sm table-borderless">
                    <tr>
                        <th width="40%">邮件通知:</th>
                        <td>
                            {% if task.send_email_notification %}
                            <span class="badge bg-success">启用</span>
                            {% else %}
                            <span class="badge bg-secondary">禁用</span>
                            {% endif %}
                        </td>
                    </tr>
                    {% if task.send_email_notification %}
                    <tr>
                        <th>成功时通知:</th>
                        <td>
                            {% if task.notify_on_success %}
                            <i class="bi bi-check-circle text-success"></i>
                            {% else %}
                            <i class="bi bi-x-circle text-muted"></i>
                            {% endif %}
                        </td>
                    </tr>
                    <tr>
                        <th>失败时通知:</th>
                        <td>
                            {% if task.notify_on_failure %}
                            <i class="bi bi-check-circle text-success"></i>
                            {% else %}
                            <i class="bi bi-x-circle text-muted"></i>
                            {% endif %}
                        </td>
                    </tr>
                    {% if task.notification_emails %}
                    <tr>
                        <th>通知邮箱:</th>
                        <td>
                            <small>{{ task.notification_emails|truncatechars:30 }}</small>
                        </td>
                    </tr>
                    {% endif %}
                    {% endif %}
                </table>
            </div>
        </div>
    </div>
</div>

<script>
function runTaskNow(taskId) {
    if (confirm('确定要立即执行这个定时任务吗？')) {
        fetch(`/scheduled-tasks/${taskId}/run-now/`, {
            method: 'POST',
            headers: {
                'X-CSRFToken': document.querySelector('[name=csrfmiddlewaretoken]'),
                'Content-Type': 'application/json',
            },
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                alert(data.message);
                location.reload();
            } else {
                alert('执行失败: ' + data.message);
            }
        })
        .catch(error => {
            console.error('Error:', error);
            alert('执行失败，请稍后重试');
        });
    }
}

function toggleTaskStatus(taskId) {
    fetch(`/scheduled-tasks/${taskId}/toggle-status/`, {
        method: 'POST',
        headers: {
            'X-CSRFToken': document.querySelector('[name=csrfmiddlewaretoken]'),
            'Content-Type': 'application/json',
        },
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            alert(data.message);
            location.reload();
        } else {
            alert('操作失败: ' + data.message);
        }
    })
    .catch(error => {
        console.error('Error:', error);
        alert('操作失败，请稍后重试');
    });
}
</script>

{% endblock %}
